package ch07 {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Vector3D;

	/**
	 * globalToLocal3D
	 * 屏幕坐标转换为局部3D坐标
	 */
	[SWF(width=800,height=800)]
	public class GlobalLocal extends Sprite {
		private var _sprite:Sprite;
		private var _tracker:Sprite;
		private var _angle:Number = 0;


		public function GlobalLocal(){
			z = 0;
			_sprite = new Sprite();
			_sprite.graphics.lineStyle(5);
			_sprite.graphics.drawRect(-200, -200, 400, 400);
			_sprite.x = 400;
			_sprite.y = 400;
			addChild(_sprite);

			_tracker = new Sprite();
			_tracker.graphics.lineStyle(2, 0xff0000);
			_tracker.graphics.drawCircle(0, 0, 20);
			_sprite.addChild(_tracker);
			//addChild(_tracker);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void {
			_sprite.rotationX += 1;
			_sprite.rotationY += 1.2;
			_sprite.rotationZ += .5;
			_sprite.x = 400 + Math.cos(_angle) * 100;
			_sprite.y = 400 + Math.sin(_angle) * 100;
			_sprite.z = 200 + Math.cos(_angle * .8) * 400;
			_angle += .05;
			trace(mouseX, mouseY);
			trace(_sprite.globalToLocal3D(new Point(mouseX, mouseY)));
			//方法一：
			//var p:Vector3D = _sprite.globalToLocal3D(new Point(mouseX, mouseY));
			//_tracker.x = p.x;
			//_tracker.y = p.y;
			/**
			 * 方法二：
			 * 了另一个更简单的方法:用旋转对象的局部鼠标坐标。
			 * 结果是如果你存取一个在3D空间里改变的对象的mouseX和mouseY参数时，
			 * 世界坐标到局部坐标的3D转换会自动进行。
			 */
			_tracker.x = _sprite.mouseX;
			_tracker.y = _sprite.mouseY;
		}

	}
}